package com.yanfan.energy.service.impl;

import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.exception.base.BaseException;
import com.yanfan.common.utils.SecurityUtils;
import com.yanfan.common.utils.StringUtils;
import com.yanfan.energy.domain.DeviceThingsEnergy;
import com.yanfan.energy.domain.model.ComboBoxBody;
import com.yanfan.energy.mapper.DeviceThingsEnergyMapper;
import com.yanfan.energy.service.DeviceThingsEnergyService;
import com.yanfan.energy.util.DateUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 设备开关机增量记录表 (DeviceThingsEnergy)表服务实现类
 *
 * @author makejava
 * @since 2023-08-03 09:27:38
 */
@Service("deviceThingsEnergyService")
public class DeviceThingsEnergyServiceImpl implements DeviceThingsEnergyService {
    @Resource
    private DeviceThingsEnergyMapper deviceThingsEnergyDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public DeviceThingsEnergy queryById(Integer id) {
        return this.deviceThingsEnergyDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param deviceThingsEnergy 筛选条件
     * @param pageRequest        分页对象
     * @return 查询结果
     */
    @Override
    public Page<DeviceThingsEnergy> queryByPage(DeviceThingsEnergy deviceThingsEnergy, PageRequest pageRequest) {
        long total = this.deviceThingsEnergyDao.count(deviceThingsEnergy);
        return new PageImpl<>(this.deviceThingsEnergyDao.queryAllByLimit(deviceThingsEnergy, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param deviceThingsEnergy 实例对象
     * @return 实例对象
     */
    @Override
    public DeviceThingsEnergy insert(DeviceThingsEnergy deviceThingsEnergy) {
        this.deviceThingsEnergyDao.insert(deviceThingsEnergy);
        return deviceThingsEnergy;
    }

    /**
     * 修改数据
     *
     * @param deviceThingsEnergy 实例对象
     * @return 实例对象
     */
    @Override
    public DeviceThingsEnergy update(DeviceThingsEnergy deviceThingsEnergy) {
        this.deviceThingsEnergyDao.update(deviceThingsEnergy);
        return this.queryById(deviceThingsEnergy.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.deviceThingsEnergyDao.deleteById(id) > 0;
    }

    @Override
    public AjaxResult findBindDeviceEnergy(Integer type, Integer id, String deviceName, Integer pageNum, Integer pageSize) {
        if (id == null) {
            throw new BaseException("查询车间的id不能为空");
        }
        PageHelper.startPage(pageNum, pageSize);
        List<DeviceThingsEnergy> result = deviceThingsEnergyDao.findDeviceThingsEnergyByCompany(type, id, deviceName);
        result.stream().forEach(re -> {
            if (re.getEnergyType() == 1) {
                re.setEnergyTypeName("冷却水");
            }
            if (re.getEnergyType() == 2) {
                re.setEnergyTypeName("软化水");
            }
            if (re.getEnergyType() == 4) {
                re.setEnergyTypeName("压缩空气");
            }
            if (re.getEnergyType() == 5) {
                re.setEnergyTypeName("天然气");
            }
            if (re.getEnergyType() == 6) {
                re.setEnergyTypeName("蒸汽");
            }
            if (re.getEnergyType() == 7) {
                re.setEnergyTypeName("尖电");
            }
            if (re.getEnergyType() == 8) {
                re.setEnergyTypeName("峰电");
            }
            if (re.getEnergyType() == 9) {
                re.setEnergyTypeName("平电");
            }
            if (re.getEnergyType() == 10) {
                re.setEnergyTypeName("谷电");
            }
        });
        PageInfo<DeviceThingsEnergy> pageInfo = new PageInfo<>(result);
        return AjaxResult.success("查询成功", pageInfo);
    }

    @Override
    public AjaxResult addBindDeviceEnergy(DeviceThingsEnergy deviceThingsEnergy) {
        // 同一个区域的设备绑定能源和物模型三个不能重复
        DeviceThingsEnergy input = new DeviceThingsEnergy();
        List<DeviceThingsEnergy> list = deviceThingsEnergyDao.findDeviceThingsEnergyById(deviceThingsEnergy.getWorkshopId(), deviceThingsEnergy
                .getEnergyType()
                .intValue(), deviceThingsEnergy.getDeviceId(), deviceThingsEnergy.getThingsModelId().intValue());
        Boolean isRepeat = list
                .stream()
                .anyMatch(x -> x.getDeviceId().equals(deviceThingsEnergy.getDeviceId()) && x
                        .getEnergyType()
                        .equals(deviceThingsEnergy.getEnergyType()) && x
                        .getThingsModelId()
                        .equals(deviceThingsEnergy.getThingsModelId()));
        if (!isRepeat) {

            // 判断设备所有在的区域是否存在
            int count = deviceThingsEnergyDao.findDeviceCount(deviceThingsEnergy.getDeviceId(), deviceThingsEnergy.getWorkshopId());
            // 没有数据就新增
            if (count > 0) {
                // 判断能源类型是否存在
                int countType = deviceThingsEnergyDao.findDeviceCountAndEnergy(deviceThingsEnergy.getDeviceId(), deviceThingsEnergy.getWorkshopId(), deviceThingsEnergy
                        .getEnergyType()
                        .intValue());
                // 如果存在就修改，如果不存在就新增
                if (countType > 0) {
                    deviceThingsEnergy.setCreatedBy(SecurityUtils.getUsername());
                    deviceThingsEnergy.setCreatedDate(DateUtils.getNowDate());
                    String deviceNumber = deviceThingsEnergyDao.findDeviceNumber(deviceThingsEnergy.getDeviceId());
                    deviceThingsEnergy.setDeviceNum(deviceNumber);
                    deviceThingsEnergyDao.updateByType(deviceThingsEnergy);
                } else {
                    deviceThingsEnergy.setCreatedBy(SecurityUtils.getUsername());
                    deviceThingsEnergy.setCreatedDate(DateUtils.getNowDate());
                    String deviceNumber = deviceThingsEnergyDao.findDeviceNumber(deviceThingsEnergy.getDeviceId());
                    deviceThingsEnergy.setDeviceNum(deviceNumber);
                    deviceThingsEnergyDao.insert(deviceThingsEnergy);
                }

                // 判断物模型是否存在
                int countThings = deviceThingsEnergyDao.findDeviceCountAndThings(deviceThingsEnergy.getDeviceId(), deviceThingsEnergy.getWorkshopId(), deviceThingsEnergy
                        .getEnergyType()
                        .intValue(), deviceThingsEnergy.getThingsModelId().intValue());

                if (countThings > 0) {
                    deviceThingsEnergy.setCreatedBy(SecurityUtils.getUsername());
                    deviceThingsEnergy.setCreatedDate(DateUtils.getNowDate());
                    String deviceNumber = deviceThingsEnergyDao.findDeviceNumber(deviceThingsEnergy.getDeviceId());
                    deviceThingsEnergy.setDeviceNum(deviceNumber);
                    deviceThingsEnergyDao.updateByType(deviceThingsEnergy);
                } else {
                    deviceThingsEnergy.setCreatedBy(SecurityUtils.getUsername());
                    deviceThingsEnergy.setCreatedDate(DateUtils.getNowDate());
                    String deviceNumber = deviceThingsEnergyDao.findDeviceNumber(deviceThingsEnergy.getDeviceId());
                    deviceThingsEnergy.setDeviceNum(deviceNumber);
                    deviceThingsEnergyDao.insert(deviceThingsEnergy);
                }

            } else {
                deviceThingsEnergy.setCreatedBy(SecurityUtils.getUsername());
                deviceThingsEnergy.setCreatedDate(DateUtils.getNowDate());
                String deviceNumber = deviceThingsEnergyDao.findDeviceNumber(deviceThingsEnergy.getDeviceId());
                deviceThingsEnergy.setDeviceNum(deviceNumber);
                deviceThingsEnergyDao.insert(deviceThingsEnergy);
            }

//            // 根据设备id查询设备所绑定的车间id---
//            Integer deviceId = deviceThingsEnergyDao.findTrueByDeviceId(deviceThingsEnergy.getDeviceId());
//
//            if (!deviceId.equals(deviceThingsEnergy.getWorkshopId())) {
//                String workshopName = deviceThingsEnergyDao.findDeviceIdWorkshopName(deviceThingsEnergy.getDeviceId());
//                throw new BaseException("所选的区域与设备所绑定的区域不一致，选择的设备应该绑定的区域为：" + workshopName);
//            }
        }

        deviceThingsEnergyDao.updateDeviceWorkshopId(deviceThingsEnergy.getDeviceId(), deviceThingsEnergy.getWorkshopId(), deviceThingsEnergy.getEnergyType());


        return AjaxResult.success(deviceThingsEnergy);
    }

    @Override
    public AjaxResult updateBindDeviceEnergy(DeviceThingsEnergy deviceThingsEnergy) {
        DeviceThingsEnergy input = new DeviceThingsEnergy();
        List<DeviceThingsEnergy> list = deviceThingsEnergyDao.findDeviceThingsEnergyById(deviceThingsEnergy.getWorkshopId(), deviceThingsEnergy
                .getEnergyType()
                .intValue(), deviceThingsEnergy.getDeviceId(), deviceThingsEnergy.getThingsModelId().intValue());
        Boolean isRepeat = list
                .stream()
                .anyMatch(x -> x.getDeviceId().equals(deviceThingsEnergy.getDeviceId()) && x
                        .getEnergyType()
                        .equals(deviceThingsEnergy.getEnergyType()) && x
                        .getThingsModelId()
                        .equals(deviceThingsEnergy.getThingsModelId()));
        if (!isRepeat) {

            // 判断设备所有在的区域是否存在
            int count = deviceThingsEnergyDao.findDeviceCount(deviceThingsEnergy.getDeviceId(), deviceThingsEnergy.getWorkshopId());
            // 没有数据就新增
            if (count > 0) {
                // 判断能源类型是否存在
                int countType = deviceThingsEnergyDao.findDeviceCountAndEnergy(deviceThingsEnergy.getDeviceId(), deviceThingsEnergy.getWorkshopId(), deviceThingsEnergy
                        .getEnergyType()
                        .intValue());
                // 如果存在就修改，如果不存在就新增
                if (countType > 0) {
                    deviceThingsEnergy.setCreatedBy(SecurityUtils.getUsername());
                    deviceThingsEnergy.setCreatedDate(DateUtils.getNowDate());
                    String deviceNumber = deviceThingsEnergyDao.findDeviceNumber(deviceThingsEnergy.getDeviceId());
                    deviceThingsEnergy.setDeviceNum(deviceNumber);
                    deviceThingsEnergyDao.updateByType(deviceThingsEnergy);
                } else {
                    deviceThingsEnergy.setCreatedBy(SecurityUtils.getUsername());
                    deviceThingsEnergy.setCreatedDate(DateUtils.getNowDate());
                    String deviceNumber = deviceThingsEnergyDao.findDeviceNumber(deviceThingsEnergy.getDeviceId());
                    deviceThingsEnergy.setDeviceNum(deviceNumber);
                    deviceThingsEnergyDao.insert(deviceThingsEnergy);
                }

                // 判断物模型是否存在
                int countThings = deviceThingsEnergyDao.findDeviceCountAndThings(deviceThingsEnergy.getDeviceId(), deviceThingsEnergy.getWorkshopId(), deviceThingsEnergy
                        .getEnergyType()
                        .intValue(), deviceThingsEnergy.getThingsModelId().intValue());

                if (countThings > 0) {
                    deviceThingsEnergy.setCreatedBy(SecurityUtils.getUsername());
                    deviceThingsEnergy.setCreatedDate(DateUtils.getNowDate());
                    String deviceNumber = deviceThingsEnergyDao.findDeviceNumber(deviceThingsEnergy.getDeviceId());
                    deviceThingsEnergy.setDeviceNum(deviceNumber);
                    deviceThingsEnergyDao.updateByType(deviceThingsEnergy);
                } else {
                    deviceThingsEnergy.setCreatedBy(SecurityUtils.getUsername());
                    deviceThingsEnergy.setCreatedDate(DateUtils.getNowDate());
                    String deviceNumber = deviceThingsEnergyDao.findDeviceNumber(deviceThingsEnergy.getDeviceId());
                    deviceThingsEnergy.setDeviceNum(deviceNumber);
                    deviceThingsEnergyDao.insert(deviceThingsEnergy);
                }

            } else {
                deviceThingsEnergy.setCreatedBy(SecurityUtils.getUsername());
                deviceThingsEnergy.setCreatedDate(DateUtils.getNowDate());
                String deviceNumber = deviceThingsEnergyDao.findDeviceNumber(deviceThingsEnergy.getDeviceId());
                deviceThingsEnergy.setDeviceNum(deviceNumber);
                deviceThingsEnergyDao.insert(deviceThingsEnergy);
            }

        }

        deviceThingsEnergyDao.updateDeviceWorkshopId(deviceThingsEnergy.getDeviceId(), deviceThingsEnergy.getWorkshopId(), deviceThingsEnergy.getEnergyType());
        return AjaxResult.success(deviceThingsEnergy);
    }

    @Override
    public AjaxResult deleteBindDeviceEnergy(Integer workshopId, Integer type, Integer id, Integer thingsId) {
        DeviceThingsEnergy deviceThingsEnergy = deviceThingsEnergyDao.queryById(id);
        if (deviceThingsEnergy != null) {
            deviceThingsEnergyDao.updateDeviceWorkshopId(deviceThingsEnergy.getDeviceId(), 0, 0l);
        }
        deviceThingsEnergyDao.deleteById(id);
        return AjaxResult.success(id);
    }

    @Override
    public List<ComboBoxBody> findIotThingsByDeviceId(Integer deviceId, String name) {
        List<ComboBoxBody> result = deviceThingsEnergyDao.findThingsModelByDeviceId(deviceId);

        if (!StringUtils.isEmpty(name)) {
            List<ComboBoxBody> result2 = result
                    .stream()
                    .filter(n -> n.getNumber().contains(name))
                    .collect(Collectors.toList());
            return result2;
        }

        return result;
    }


}
